package com.daqsoft.controller.process;

import com.daqsoft.data.Message;
import com.daqsoft.data.User;
import com.daqsoft.data.project.Project;
import com.daqsoft.data.project.ProjectUser;
import com.daqsoft.data.project.Task;
import com.daqsoft.dto.GeneralResultEnum;
import com.daqsoft.dto.StatusEnum;
import com.daqsoft.permission.Verify;
import com.daqsoft.permission.VerifyType;
import com.daqsoft.service.NoticeService;
import com.daqsoft.service.process.ProjectConfimProcessService;
import com.daqsoft.service.project.ProjectService;
import com.daqsoft.service.project.TaskService;
import com.daqsoft.service.system.UserManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 项目计划确认流程
 * 流程: 项目经理发起提交项目 -> 各个负责人确认任务 -> 项目开始发布所有任务
 * @author 周麟东 on 9:32
 */
@RestController
@RequestMapping("projectConfimProcess")
public class ProjectConfirmProcessController {
    @Autowired
    private ProjectService projectService;
    @Autowired
    private ProjectConfimProcessService confimService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private UserManageService userService;
    @Autowired
    private NoticeService noticeService;

    @RequestMapping("submitProject")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "提交项目")
    public GeneralResultEnum submitProject(HttpServletRequest request , int projectId){
        User user = (User) request.getSession().getAttribute("user");
        Project project =  projectService.get(projectId);
        if (project == null ){
            return GeneralResultEnum.FAIL.setDesc("该项目已经不存在");
        }
        if (project.getStatus() != StatusEnum.WAIT){
            return GeneralResultEnum.FAIL.setDesc("该项目已经提交过了!");
        }
        if (project.getPm().getId() != user.getId()){
            return GeneralResultEnum.FAIL.setDesc("你不是该项目的项目经理 , 不能提交项目");
        }
        confimService.submitProject(project);
        return GeneralResultEnum.SUCCESS.setDesc("提交完成");
    }

    /**
     * 各项目经理查看当前确认进度
     * @param projectId 项目ID
     * @return 各项目经理确认情况
     */
    @RequestMapping("getConfirmProcess")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "获取确认进度")
    public GeneralResultEnum getConfirmProcess(int projectId){
        Project project = projectService.get(projectId);
        if (project == null){
            return GeneralResultEnum.FAIL.setDesc("该项目已经不存在");
        }
        List<ProjectUser> projectUsers = projectService.getProjectUserByProject(project);
        List<Map<String,Object>> list = new ArrayList<>();
        for (ProjectUser projectUser : projectUsers) {
            list.add(projectUser.toMap());
        }
        return GeneralResultEnum.SUCCESS.setData(list);
    }

    /**
     * 项目经理开始项目
     * @param projectId 项目ID
     * @return 开始是否成功
     */
    @RequestMapping("startProject")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "开始项目")
    public GeneralResultEnum startProject(HttpServletRequest request , int projectId){
        User user = (User) request.getSession().getAttribute("user");
        Project project = projectService.get(projectId);
        if (project == null){
            return GeneralResultEnum.FAIL.setDesc("该项目已经不存在");
        }
        if (project.getPm().getId() != user.getId()){
            return GeneralResultEnum.FAIL.setDesc("你不是该项目的项目经理 , 不能开始项目");
        }
        project.setStatus(StatusEnum.PROCESS);
        projectService.save(project);
        return GeneralResultEnum.SUCCESS.setDesc("开始项目成功");
    }

    /**
     * 各个责任人指派任务
     * @param id 任务ID
     * @param principalId 指派给谁的ID
     * @return 是否指派成功
     */
    @RequestMapping("designateTask")
    @Verify(value = VerifyType.HAVE_PERMISSION, desc = "指派任务")
    public GeneralResultEnum designateTask(HttpServletRequest request, int id, int principalId) {
        User self = (User) request.getSession().getAttribute("user");
        Task task = taskService.get(id);
        if (task == null) {
            return GeneralResultEnum.FAIL.setDesc("该任务不存在");
        }
        User user = userService.findOne(principalId);
        if (user == null) {
            return GeneralResultEnum.FAIL.setDesc("被指派者不存在");
        }
        task.setPrincipal(user);
        task = taskService.save(task);
        noticeService.sendMessage(new Message(self, user, "你被指派完成任务:[" + task.getName() + "]"));
        return GeneralResultEnum.SUCCESS.setData(task.toMap()).setDesc("指派成功");
    }

    /**
     * 确认项目
     * @param projectId 被确认的项目ID
     * @param taskId 确认任务ID
     * @return 是否确认成功
     */
    @RequestMapping("confirmProject")
    @Verify(value = VerifyType.HAVE_PERMISSION, desc = "确认项目")
    public GeneralResultEnum confirmProject(HttpServletRequest request, int projectId, int taskId) {
        User user = (User) request.getSession().getAttribute("user");
        Project project = projectService.get(projectId);
        if (project == null) {
            return GeneralResultEnum.FAIL.setDesc("该项目已经不存在");
        }
        Task task = taskService.get(taskId);
        //更新确认人
        confimService.confirmProject(project, user , task);
        return GeneralResultEnum.SUCCESS;
    }
}
